# coding=utf-8
#

from datetime import datetime

from django.db import models


class Pool(models.Model):
    """ 矿池 """
    pid = models.CharField(max_length=128, verbose_name="矿池ID")
    pool_user_id = models.CharField(max_length=128, verbose_name="用户在矿池中的ID")
    name = models.CharField(max_length=128, verbose_name="矿池名称")
    create_time = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    class Meta:
        ordering = ("-create_time",)
        verbose_name = "矿池"
        verbose_name_plural = "矿池管理"

    def __str__(self):
        return self.name


class Agent(models.Model):
    """ 挖矿agent """
    STATUS_CHOICES = ((1, "运行中"),
                      (0, "已下线"),
                      (-2, "已故障"),
                      (-10, "已废弃"))

    idc = models.CharField(max_length=32, verbose_name="运营商", default="")
    cpu = models.IntegerField(default=0, verbose_name="CPU核数")
    local_its = models.FloatField(default=0, verbose_name="本地算力")
    local_avg_its = models.FloatField(default=0, verbose_name="本地平均算力")
    ip = models.CharField(max_length=64, verbose_name="公网IP", default="")

    alias = models.CharField(max_length=128, verbose_name="agent别名")
    version = models.CharField(max_length=64, verbose_name="软件版本")

    sols = models.FloatField(default=0, verbose_name="sol数量")
    its = models.FloatField(default=0, verbose_name="算力")
    is_active = models.BooleanField(default=False, verbose_name="是否活跃")
    server_id = models.CharField(max_length=64, verbose_name="HW云用的ServerID", default="")
    project_id = models.CharField(max_length=64, verbose_name="HW云用的ProjectID", default="")
    az = models.CharField(max_length=64, verbose_name="HW云用的AZ", default="")
    last_active = models.DateTimeField(default=datetime(2000, 1, 1), verbose_name="最后的活跃时间")
    status = models.IntegerField(default=0, choices=STATUS_CHOICES, verbose_name="Agent状态")
    create_time = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    class Meta:
        ordering = ("-create_time",)
        verbose_name = "矿工Agent"
        verbose_name_plural = "矿工Agent管理"

        indexes = [
            models.Index(fields=["alias"], name="agent_alias"),
            models.Index(fields=["idc"], name="agent_idc"),
            models.Index(fields=["ip"], name="agent_public_ip"),
            models.Index(fields=["local_its"], name="agent_local_its"),
        ]

    def __str__(self):
        return self.alias


class AgentHeartbeat(models.Model):
    """ agent心跳 """
    alias = models.CharField(max_length=128, verbose_name="agent别名")
    cpu = models.IntegerField(default=0, verbose_name="CPU")
    ip = models.CharField(max_length=64, verbose_name="IP", default="")
    local_its = models.FloatField(default=0, verbose_name="本地算力")
    local_avg_its = models.FloatField(default=0, verbose_name="本地平均算力")
    cur_time = models.DateTimeField(verbose_name="当前时间")
    create_time = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="修改时间")

    class Meta:
        ordering = ("-create_time",)
        verbose_name = "Agent心跳"
        verbose_name_plural = "Agent心跳管理"

        indexes = [
            models.Index(fields=["alias"], name="agent_heartbeat_alias"),
        ]

    def __str__(self):
        return self.alias

